//
//  CYShopCartBottomView.m
//  CaiYun
//
//  Created by 今日信息科技 on 2017/7/31.
//  Copyright © 2017年 今日信息科技. All rights reserved.
//

#import "CYShopCartBottomView.h"
#import "CYShopCartBottomBtn.h"
@interface CYShopCartBottomView(){
    ShopCartBottomViewBlock _myblock;
}
@property (nonatomic, strong) CYShopCartBottomBtn *timeBtn;//加减倍数按钮
@property (nonatomic, strong) CYLabel *leftTitleLBL;//投
@property (nonatomic, strong) CYLabel *rightTitleLBL;//倍
@property (nonatomic, strong) CYButton *clearBtn;//清空按钮
@property (nonatomic, strong) CYButton *payBtn;//付款按钮
@property (nonatomic, strong) CYView *midLine;//
@property (nonatomic, strong) CYLabel *moneyLBL;//可用余额
@property (nonatomic, strong) CYLabel *bettingNumLBL;//投注数
@end
@implementation CYShopCartBottomView
- (instancetype)initWithColor:(UIColor *)color block:(ShopCartBottomViewBlock)block{
    self = [super initWithColor:color];
    if (self) {
        _myblock = block;
        self.money = [CYUser shareUser].coin;//可用余额
        self.bounds = CGRectMake(0.0, 0.0, SCREEN_WIDTH, (AL(PT(66)) + AL(PT(20)) * 2) * 2);
        self.timeBtn.hidden = self.leftTitleLBL.hidden = self.rightTitleLBL.hidden = self.midLine.hidden = self.clearBtn.hidden = self.payBtn.hidden = NO;
        self.moneyLBL.hidden = self.bettingNumLBL.hidden = NO;
    }
    return self;
}
- (CYShopCartBottomBtn *)timeBtn{
    if (!_timeBtn) {
        WS(self)
        _timeBtn = [[CYShopCartBottomBtn alloc]initWithColor:[UIColor whiteColor] block:^(int times) {
            [weakSelf minusOrPlusAction:times];
        }];
        _timeBtn.isChangeTimes = NO;
        [self addSubview:_timeBtn];
        
        [_timeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self).with.offset(AL(PT(20)));
            make.centerX.equalTo(self);
            make.size.mas_equalTo(CGSizeMake(AL(PT(160)) + AL(PT(66)) * 2, AL(PT(66))));
        }];
    }
    return _timeBtn;
}

- (CYLabel *)leftTitleLBL{
    if (!_leftTitleLBL) {
        _leftTitleLBL = [[CYLabel alloc]initWithText:@"投" size:AL(PT(30)) textcolor:ColorHex(0x737373)];
        [self addSubview:_leftTitleLBL];
        
        [_leftTitleLBL mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(_timeBtn.mas_left).with.offset(-AL(PT(10)));
            make.centerY.equalTo(_timeBtn);
            make.size.mas_equalTo(_leftTitleLBL.bounds.size);
        }];
    }
    return _leftTitleLBL;
}

- (CYLabel *)rightTitleLBL{
    if (!_rightTitleLBL) {
        _rightTitleLBL = [[CYLabel alloc]initWithText:@"倍" size:AL(PT(30)) textcolor:ColorHex(0x737373)];
        [self addSubview:_rightTitleLBL];
        
        [_rightTitleLBL mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(_timeBtn.mas_right).with.offset(AL(PT(10)));
            make.centerY.equalTo(_timeBtn);
            make.size.mas_equalTo(_rightTitleLBL.bounds.size);
        }];
    }
    return _rightTitleLBL;
}

- (CYView *)midLine{
    if (!_midLine) {
        _midLine = [[CYView alloc]initWithColor:ColorHex(0xe0e0e0)];
        [self addSubview:_midLine];
        
        [_midLine mas_makeConstraints:^(MASConstraintMaker *make) {
            make.center.equalTo(self);
            make.size.mas_equalTo(CGSizeMake(SCREEN_WIDTH, 0.5));
        }];
    }
    return _midLine;
}

- (CYButton *)clearBtn{
    if (!_clearBtn) {
        _clearBtn = [[CYButton alloc]initWithTitle:@"清空" titleColor:ColorHex(0x737373) backgroundColor:[UIColor whiteColor] cornerRadius:0.0];
        [_clearBtn addTarget:self action:@selector(actionForClearBtn) forControlEvents:UIControlEventTouchUpInside];
        [_clearBtn setTitleColor:ColorHex(0xe0e0e0) forState:UIControlStateHighlighted];
        _clearBtn.titleLabel.font = [UIFont systemFontOfSize:AL(PT(30))];
        [self addSubview:_clearBtn];
        [self setupRightLine];
        
        [_clearBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self).with.offset(0);
            make.top.equalTo(_midLine.mas_bottom).with.offset(0);
            make.size.mas_equalTo(CGSizeMake(AL(PT(66)) + AL(PT(20)) * 2, AL(PT(66)) + AL(PT(20)) * 2));
        }];
    }
    return _clearBtn;
}

- (CYButton *)payBtn{
    if (!_payBtn) {
        _payBtn = [[CYButton alloc]initWithTitle:@"付款" titleColor:[UIColor whiteColor] backgroundColor:THEME_COLOR cornerRadius:0.0];
        [_payBtn addTarget:self action:@selector(actionForPayBtn) forControlEvents:UIControlEventTouchUpInside];
        [_payBtn setTitleColor:ColorHex(0xe0e0e0) forState:UIControlStateHighlighted];
        _payBtn.titleLabel.font = [UIFont systemFontOfSize:AL(PT(30))];
        [self addSubview:_payBtn];
        
        [_payBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(self).with.offset(0);
            make.bottom.equalTo(self).with.offset(0);
            make.size.mas_equalTo(CGSizeMake((AL(PT(66)) + AL(PT(20)) * 2) * 1.5, AL(PT(66)) + AL(PT(20)) * 2));
        }];
    }
    
    return _payBtn;
}

- (CYLabel *)moneyLBL{
    if (!_moneyLBL) {
        _moneyLBL = [[CYLabel alloc]initWithText:[NSString stringWithFormat:@"可用余额:%.2f元",_money] size:AL(PT(26)) textcolor:ColorHex(0x737373)];
        [self addSubview:_moneyLBL];
        
        [_moneyLBL mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(self);
            make.bottom.equalTo(self).with.offset(- (AL(PT(66)) / 2.0 + AL(PT(20)) - _moneyLBL.bounds.size.height));
            make.size.mas_equalTo(_moneyLBL.bounds.size);
        }];
    }
    return _moneyLBL;
}

- (CYLabel *)bettingNumLBL{
    
    if (!_bettingNumLBL) {
        _bettingNumLBL = [[CYLabel alloc]initWithText:[NSString stringWithFormat:@"%d注%d元", _bettingNum, _bettingMoney]
                                                 size:AL(PT(26)) textcolor:ColorHex(0x333333)];
        [self addSubview:_bettingNumLBL];

        [_bettingNumLBL mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(self);
            make.bottom.equalTo(self.moneyLBL.mas_top).with.offset(0);
            make.size.mas_equalTo(_bettingNumLBL.bounds.size);
        }];
 [self setupMoneyLBL];
    }
    return _bettingNumLBL;
}

- (void)setMoney:(float)money{
    if (_money != money){
        _money = money;
        self.moneyLBL.text = [NSString stringWithFormat:@"可用余额:%.2f元",_money];
        [_moneyLBL mas_updateConstraints:^(MASConstraintMaker *make) {
            make.size.mas_equalTo(_moneyLBL.bounds.size);
        }];
    }
}

- (void)setBettingMoney:(int)bettingMoney{
    if (_bettingMoney != bettingMoney){
        _bettingMoney = bettingMoney;
        [self setupMoneyLBL];
    }
}

- (void)setupRightLine{
    CYView *line= [[CYView alloc]initWithColor:ColorHex(0xe0e0e0)];
    [_clearBtn addSubview:line];
    
    [line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_offset(UIEdgeInsetsMake(AL(PT(20)), AL(PT(66)) + AL(PT(20)) * 2 - 0.5, AL(PT(20)), 0.0));
    }];
}

//设置投注数目和投注金额是特定的颜色，与字体
- (void)setupMoneyLBL{
    NSString *bettingNum_string = [NSString stringWithFormat:@"%d",_bettingNum];//注数
    //    NSString *bettingTimes_string = [NSString stringWithFormat:@"%d",_bettingTimes];//倍数
    NSString *bettingMoney_string = [NSString stringWithFormat:@"%d",_bettingMoney];//金额
    
    NSString *string = [NSString stringWithFormat:@"%@注%@元", bettingNum_string, bettingMoney_string];
    NSMutableAttributedString *attributedStr  = [[NSMutableAttributedString alloc]initWithString:string];
    
    [attributedStr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:AL(PT(26))] range:NSMakeRange(0, bettingNum_string.length)];
    [attributedStr addAttribute:NSForegroundColorAttributeName value:THEME_COLOR range:NSMakeRange(0, bettingNum_string.length)];
    
    //    [attributedStr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:AL(PT(26))] range:NSMakeRange(1 + bettingNum_string.length, bettingTimes_string.length)];
    //    [attributedStr addAttribute:NSForegroundColorAttributeName value:THEME_COLOR range:NSMakeRange(1 + bettingNum_string.length, bettingTimes_string.length)];
    
    [attributedStr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:AL(PT(26))] range:NSMakeRange(1 + bettingNum_string.length, bettingMoney_string.length)];
    [attributedStr addAttribute:NSForegroundColorAttributeName value:THEME_COLOR range:NSMakeRange(1 + bettingNum_string.length, bettingMoney_string.length)];
    
    self.bettingNumLBL.attributedText = attributedStr;
    
    CGSize size = [string sizeWithAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:AL(PT(26))]}];
    CGFloat width = ceil(size.width);
    [_bettingNumLBL mas_updateConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(width, CGRectGetHeight(_bettingNumLBL.bounds)));
    }];
    
}

//清空
- (void)actionForClearBtn{
    _myblock(CleanAction,0);
}

//付款
- (void)actionForPayBtn{
    _myblock(PayAction,0);
}

//减或者加
- (void)minusOrPlusAction:(int)times{
    _myblock(MinusOrPlusAction,times);
}

/*
 // Only override drawRect: if you perform custom drawing.
 // An empty implementation adversely affects performance during animation.
 - (void)drawRect:(CGRect)rect {
 // Drawing code
 }
 */

@end
